﻿
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class snapshot
    Inherits cacheProcessor

#If DEBUG Then
    Public Const threadCounts As Int64 = 1
#Else
    Public Const threadCounts As Int64 = 8
#End If
    Public Const _pathidentity As String = "/snapshot"
    Public Const urlquery As String = "url"
    Public Const widthquery As String = "width"
    Public Const heightquery As String = "height"
    Public Const destwidthquery As String = "destwidth"
    Public Const destheightquery As String = "destheight"
    Private drawer(threadCounts - 1) As webBrowserDrawer

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Sub addresponseHeaders(ByVal context As httpContext)
        If isemptystring(context.url.query(urlquery)) Then
            context.setcontentEncoding(encoding.utf8)
            context.setcontentType(network.http.contentType.texthtmlUtf8)
        Else
            context.setcontentEncoding(encoding.utf8)
            context.setcontentType(network.http.contentType.imagejpeg)
        End If
    End Sub

    Protected Overrides Function processImpl(ByVal context As httpContext) As Boolean
        Dim url As String = Nothing
        url = context.url.query(urlquery)
        If isemptystring(url) Then
            Dim page As xhtmlpage = Nothing
            page = New xhtmlpage(context.buff())
            page.title().text = applicationSign() + " - snapshot - input url"

            Dim form As form = Nothing
            form = New form(context.buff())
            form.action = strmid(_pathidentity, strlen(network.pathSeperator))
            page.body().addchild(form)

            Dim label As label = Nothing
            label = New label(context.buff())
            label.text = "please input link here."
            form.addchild(label)

            form.addchild(New br(context.buff()))

            Dim input As input = Nothing
            input = New input(context.buff())
            input.type = xhtmlctrl.input.type_enum.text
            input.name = urlquery
            form.addchild(input)

            form.addchild(New br(context.buff()))

            input = New input(context.buff())
            input.type = xhtmlctrl.input.type_enum.submit
            input.value = "submit"
            form.addchild(input)

            Return page.render()
        Else
            Dim i As Int64
            For i = 0 To threadCounts - 1
                If Not drawer(i).busy Then
                    Exit For
                End If
            Next
            If i = threadCounts Then
                i = rnd(0, threadCounts, True)
            End If

            Dim widthStr As String = Nothing
            Dim heightStr As String = Nothing
            widthStr = context.url().query(widthquery)
            heightStr = context.url().query(heightquery)
            Dim width As Int64
            Dim height As Int64
            If Not convertor.toint64(widthStr, width) Then
                width = npos
            End If
            If Not convertor.toint64(heightStr, height) Then
                height = npos
            End If
            widthStr = context.url().query(destwidthquery)
            heightStr = context.url().query(destheightquery)
            Dim destWidth As Int64
            Dim destHeight As Int64
            If Not convertor.toint64(widthStr, destWidth) Then
                destWidth = npos
            End If
            If Not convertor.toint64(heightStr, destHeight) Then
                destHeight = npos
            End If

            If Not drawer(i).save(url, context.buff(), width, height, destWidth, destHeight) Then
                context.buff().Position = 0
                Return False
            Else
                Return True
            End If
        End If
    End Function

    Public Sub New()
        Dim i As Int64
        For i = 0 To threadCounts - 1
            drawer(i) = New webBrowserDrawer()
            drawer(i).show()
        Next
    End Sub

    Protected Overrides Sub closeImpl()
        Dim i As Int64
        For i = 0 To threadCounts - 1
            drawer(i).close()
        Next
        MyBase.closeImpl()
    End Sub

    Protected Overrides Sub Finalize()
        close()
        MyBase.Finalize()
    End Sub
End Class
